home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 June: Reference Library / Dev.CD Jun 99 RL Disk 1.toast / What's New / Development Kits / Mac_OS_USB_DDK_v1.2 / Examples / USBKeypad / USBKeypadKeyIn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-04-15  |  22.6 KB  |  935 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        USBKeypadKeyIn.c
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Version:    xxx put version here xxx
  7.  
  8.     Copyright:    © 1999 by Apple Computer, Inc., all rights reserved.
  9.  
  10. */
  11.  
  12. /*
  13.     File:        KeyIn.c
  14.  
  15.     Contains:    ADB keyboard simulation (non-shim based).
  16.  
  17.     Version:    xxx put version here xxx
  18.  
  19.     Copyright:    © 1998-1999 by Apple Computer, Inc., all rights reserved.
  20.  
  21. */
  22.  
  23. /*
  24.  USB Keyboard Translation to Macintosh 
  25.  */
  26.  
  27. #include <Types.h>
  28. #include <Events.h>
  29. #include <Resources.h>
  30. #include <LowMem.h>
  31. #include <USB.h>
  32. #include "USBKeypad.h"
  33.  
  34. extern    usbMacAllyPBStruct myParamBlock;
  35. extern    usbMacAllyPBStruct shimParamBlock;
  36.  
  37.  
  38. #define DOWN        0
  39. #define UP            1
  40.  
  41. #define TRUE        1
  42. #define FALSE        0
  43.  
  44. #define FakeADBAddr        16
  45. #define FakeKBDType        2        // this should be the same as the Apple extended keyboard for now
  46.  
  47. static UInt32    myKeyMAP[4];
  48. static UInt32    keyTransState;
  49. static Handle    handleKCHR;
  50. static UInt8*    KCHRptr;
  51.  
  52. typedef KeyMap * KeyMapPtr;
  53.  
  54. /* prototypes */
  55. Boolean KeyInArray(UInt8 key, UInt8 *array, UInt16 len);
  56. Boolean SetBit(UInt8 *bitmapArray, UInt16 index, Boolean value); 
  57. void  PostADBKeyToMac(UInt16 virtualKeycode, UInt8 state);
  58.  
  59. /* when we move to master interfaces we can get this stuff from LowMemPriv.h */
  60. /* be sure to turn on DIRECT_LOWMEM_ACCESSORS */
  61. #define LMSetKbdVars(value) ((*(short *)0x0216) = (value))
  62. #define LMSetKeyLast(value) ((*(short *)0x0184) = (value))
  63. #define LMSetKeyTime(value) ((*(long *)0x0186) = (value))
  64. #define LMSetKeyRepTime(value) ((*(long *)0x018A) = (value))
  65. #define LMSetKeyMap(KeyMapValue)    BlockMove((Ptr)(KeyMapValue), (Ptr)0x0174, sizeof(KeyMap))
  66.  
  67. // index represents USB keyboard usage value, content is Mac virtual keycode
  68. static UInt8    USBKMAPNumLockDown[256] = {  
  69.     0xFF,     /* 00 no event */        
  70.     0xFF,    /* 01 ErrorRollOver */    
  71.     0xFF,    /* 02 POSTFail */    
  72.     0xFF,    /* 03 ErrorUndefined */    
  73.     0x00,    /* 04 A */
  74.     0x0B,    /* 05 B */
  75.     0x08,    /* 06 C */
  76.     0x02,    /* 07 D */
  77.     0x0E,    /* 08 E */
  78.     0x03,    /* 09 F */
  79.     0x05,    /* 0A G */
  80.     0x04,    /* 0B H */
  81.     0x22,    /* 0C I */
  82.     0x26,    /* 0D J */
  83.     0x28,    /* 0E K */
  84.     0x25,    /* 0F L */
  85.  
  86.     0x2E,     /* 10 M */        
  87.     0x2D,    /* 11 N */    
  88.     0x1F,    /* 12 O */    
  89.     0x23,    /* 13 P */    
  90.     0x0C,    /* 14 Q */
  91.     0x0F,    /* 15 R */
  92.     0x01,    /* 16 S */
  93.     0x11,    /* 17 T */
  94.     0x20,    /* 18 U */
  95.     0x09,    /* 19 V */
  96.     0x0D,    /* 1A W */
  97.     0x07,    /* 1B X */
  98.     0x10,    /* 1C Y */
  99.     0x06,    /* 1D Z */
  100.     0x12,    /* 1E 1/! */
  101.     0x13,    /* 1F 2/@ */
  102.  
  103.     0x14,     /* 20 3 # */        
  104.     0x15,    /* 21 4 $ */    
  105.     0x17,    /* 22 5 % */    
  106.     0x16,    /* 23 6 ^ */    
  107.     0x1A,    /* 24 7 & */
  108.     0x1C,    /* 25 8 * */
  109.     0x19,    /* 26 9 ( */
  110.     0x1D,    /* 27 0 ) */
  111.     0x24,    /* 28 Return (Enter) */
  112.     0x35,    /* 29 ESC */
  113.     0x33,    /* 2A Delete (Backspace) */
  114.     0x30,    /* 2B Tab */
  115.     0x31,    /* 2C Spacebar */
  116.     0x1B,    /* 2D - _ */
  117.     0x18,    /* 2E = + */
  118.     0x21,    /* 2F [ { */
  119.  
  120.     0x1E,     /* 30 ] } */        
  121.     0x2A,    /* 31 \ | */    
  122.     0xFF,    /* 32 Non-US # and ~ (what?!!!) */    
  123.     0x29,    /* 33 ; : */    
  124.     0x27,    /* 34 ' " */
  125.     0x32,    /* 35 ` ~ */
  126.     0x2B,    /* 36 , < */
  127.     0x2F,    /* 37 . > */
  128.     0x2C,    /* 38 / ? */
  129.     0x39,    /* 39 Caps Lock */
  130.     0x7A,    /* 3A F1 */
  131.     0x78,    /* 3B F2 */
  132.     0x63,    /* 3C F3 */
  133.     0x76,    /* 3D F4 */
  134.     0x60,    /* 3E F5 */
  135.     0x61,    /* 3F F6 */
  136.  
  137.     0x62,     /* 40 F7 */        
  138.     0x64,    /* 41 F8 */    
  139.     0x65,    /* 42 F9 */    
  140.     0x6D,    /* 43 F10 */    
  141.     0x67,    /* 44 F11 */
  142.     0x6F,    /* 45 F12 */
  143.     0x69,    /* 46 F13/PrintScreen */
  144.     0x6B,    /* 47 F14/ScrollLock */
  145.     0x71,    /* 48 F15/Pause */                
  146.     0x72,    /* 49 Insert */
  147.     0x73,    /* 4A Home */
  148.     0x74,    /* 4B PageUp */
  149.     0x75,    /* 4C Delete Forward */
  150.     0x77,    /* 4D End */
  151.     0x79,    /* 4E PageDown */
  152.     0x7C,    /* 4F RightArrow */
  153.  
  154.     0x7B,     /* 50 LeftArrow */        
  155.     0x7D,    /* 51 DownArrow */    
  156.     0x7E,    /* 52 UpArrow */    
  157.     0x47,    /* 53 NumLock/Clear */    
  158.     0x4B,    /* 54 Keypad / */
  159.     0x43,    /* 55 Keypad * */
  160.     0x4E,    /* 56 Keypad - */
  161.     0x45,    /* 57 Keypad + */
  162.     0x4C,    /* 58 Keypad Enter */
  163.     0x53,    /* 59 Keypad 1 */
  164.     0x54,    /* 5A Keypad 2 */
  165.     0x55,    /* 5B Keypad 3 */
  166.     0x56,    /* 5C Keypad 4 */
  167.     0x57,    /* 5D Keypad 5 */
  168.     0x58,    /* 5E Keypad 6 */
  169.     0x59,    /* 5F Keypad 7 */
  170.  
  171.     0x5B,     /* 60 Keypad 8 */        
  172.     0x5C,    /* 61 Keypad 9 */    
  173.     0x52,    /* 62 Keypad 0 */    
  174.     0x41,    /* 63 Keypad . */    
  175.     0xFF,    /* 64 Non-US \ and  | (what ??!!) */
  176.     0x6E,    /* 65 ApplicationKey (not on a mac!)*/
  177.     0x7F,    /* 66 PowerKey  */
  178.     0x51,    /* 67 Keypad = */
  179.     0x69,    /* 68 F13 */
  180.     0x6B,    /* 69 F14 */
  181.     0x71,    /* 6A F15 */
  182.     0xFF,    /* 6B F16 */
  183.     0xFF,    /* 6C F17 */
  184.     0xFF,    /* 6D F18 */
  185.     0xFF,    /* 6E F19 */
  186.     0xFF,    /* 6F F20 */
  187.  
  188.     0x5B,     /* 70 F21 */        
  189.     0x5C,    /* 71 F22 */    
  190.     0x52,    /* 72 F23 */    
  191.     0x41,    /* 73 F24 */    
  192.     0xFF,    /* 74 Execute */
  193.     0xFF,    /* 75 Help */
  194.     0x7F,    /* 76 Menu */
  195.     0x4C,    /* 77 Select */
  196.     0x69,    /* 78 Stop */
  197.     0x6B,    /* 79 Again */
  198.     0x71,    /* 7A Undo */
  199.     0xFF,    /* 7B Cut */
  200.     0xFF,    /* 7C Copy */
  201.     0xFF,    /* 7D Paste */
  202.     0xFF,    /* 7E Find */
  203.     0xFF,    /* 7F Mute */
  204.     
  205.     0xFF,     /* 80 no event */        
  206.     0xFF,    /* 81 no event */    
  207.     0xFF,    /* 82 no event */    
  208.     0xFF,    /* 83 no event */    
  209.     0xFF,    /* 84 no event */
  210.     0xFF,    /* 85 no event */
  211.     0xFF,    /* 86 no event */
  212.     0xFF,    /* 87 no event */
  213.     0xFF,    /* 88 no event */
  214.     0xFF,    /* 89 no event */
  215.     0xFF,    /* 8A no event */
  216.     0xFF,    /* 8B no event */
  217.     0xFF,    /* 8C no event */
  218.     0xFF,    /* 8D no event */
  219.     0xFF,    /* 8E no event */
  220.     0xFF,    /* 8F no event */
  221.  
  222.     0xFF,     /* 90 no event */        
  223.     0xFF,    /* 91 no event */    
  224.     0xFF,    /* 92 no event */    
  225.     0xFF,    /* 93 no event */    
  226.     0xFF,    /* 94 no event */
  227.     0xFF,    /* 95 no event */
  228.     0xFF,    /* 96 no event */
  229.     0xFF,    /* 97 no event */
  230.     0xFF,    /* 98 no event */
  231.     0xFF,    /* 99 no event */
  232.     0xFF,    /* 9A no event */
  233.     0xFF,    /* 9B no event */
  234.     0xFF,    /* 9C no event */
  235.     0xFF,    /* 9D no event */
  236.     0xFF,    /* 9E no event */
  237.     0xFF,    /* 9F no event */
  238.  
  239.     0xFF,     /* A0 no event */        
  240.     0xFF,    /* A1 no event */    
  241.     0xFF,    /* A2 no event */    
  242.     0xFF,    /* A3 no event */    
  243.     0xFF,    /* A4 no event */
  244.     0xFF,    /* A5 no event */
  245.     0xFF,    /* A6 no event */
  246.     0xFF,    /* A7 no event */
  247.     0xFF,    /* A8 no event */
  248.     0xFF,    /* A9 no event */
  249.     0xFF,    /* AA no event */
  250.     0xFF,    /* AB no event */
  251.     0xFF,    /* AC no event */
  252.     0xFF,    /* AD no event */
  253.     0xFF,    /* AE no event */
  254.     0xFF,    /* AF no event */
  255.  
  256.     0xFF,     /* B0 no event */        
  257.     0xFF,    /* B1 no event */    
  258.     0xFF,    /* B2 no event */    
  259.     0xFF,    /* B3 no event */    
  260.     0xFF,    /* B4 no event */
  261.     0xFF,    /* B5 no event */
  262.     0xFF,    /* B6 no event */
  263.     0xFF,    /* B7 no event */
  264.     0xFF,    /* B8 no event */
  265.     0xFF,    /* B9 no event */
  266.     0xFF,    /* BA no event */
  267.     0xFF,    /* BB no event */
  268.     0xFF,    /* BC no event */
  269.     0xFF,    /* BD no event */
  270.     0xFF,    /* BE no event */
  271.     0xFF,    /* BF no event */
  272.  
  273.     0xFF,     /* C0 no event */        
  274.     0xFF,    /* C1 no event */    
  275.     0xFF,    /* C2 no event */    
  276.     0xFF,    /* C3 no event */    
  277.     0xFF,    /* C4 no event */
  278.     0xFF,    /* C5 no event */
  279.     0xFF,    /* C6 no event */
  280.     0xFF,    /* C7 no event */
  281.     0xFF,    /* C8 no event */
  282.     0xFF,    /* C9 no event */
  283.     0xFF,    /* CA no event */
  284.     0xFF,    /* CB no event */
  285.     0xFF,    /* CC no event */
  286.     0xFF,    /* CD no event */
  287.     0xFF,    /* CE no event */
  288.     0xFF,    /* CF no event */
  289.  
  290.     0xFF,     /* D0 no event */        
  291.     0xFF,    /* D1 no event */    
  292.     0xFF,    /* D2 no event */    
  293.     0xFF,    /* D3 no event */    
  294.     0xFF,    /* D4 no event */
  295.     0xFF,    /* D5 no event */
  296.     0xFF,    /* D6 no event */
  297.     0xFF,    /* D7 no event */
  298.     0xFF,    /* D8 no event */
  299.     0xFF,    /* D9 no event */
  300.     0xFF,    /* DA no event */
  301.     0xFF,    /* DB no event */
  302.     0xFF,    /* DC no event */
  303.     0xFF,    /* DD no event */
  304.     0xFF,    /* DE no event */
  305.     0xFF,    /* DF no event */
  306.  
  307.     0x3B,     /* E0 left control key */        
  308.     0x38,    /* E1 left shift key key */    
  309.     0x3A,    /* E2 left alt/option key */    
  310.     0x37,    /* E3 left GUI (windows/cmd) key */    
  311.     
  312.     0x3E,    /* E4 right control key */
  313.     0x3C,    /* E5 right shift key key */
  314.     0x3D,    /* E6 right alt/option key */
  315.     0x37,    /* E7 right GUI (windows/cmd) key */
  316.     0xFF,    /* E8 no event */
  317.     0xFF,    /* E9 no event */
  318.     0xFF,    /* EA no event */
  319.     0xFF,    /* EB no event */
  320.     0xFF,    /* EC no event */
  321.     0xFF,    /* ED no event */
  322.     0xFF,    /* EE no event */
  323.     0xFF,    /* EF no event */
  324.     
  325.     0xFF,     /* F0 no event */        
  326.     0xFF,    /* F1 no event */    
  327.     0xFF,    /* F2 no event */    
  328.     0xFF,    /* F3 no event */    
  329.     0xFF,    /* F4 no event */
  330.     0xFF,    /* F5 no event */
  331.     0xFF,    /* F6 no event */
  332.     0xFF,    /* F7 no event */
  333.     0xFF,    /* F8 no event */
  334.     0xFF,    /* F9 no event */
  335.     0xFF,    /* FA no event */
  336.     0xFF,    /* FB no event */
  337.     0xFF,    /* FC no event */
  338.     0xFF,    /* FD no event */
  339.     0xFF,    /* FE no event */
  340.     0xFF,    /* FF no event */
  341. };
  342.         
  343. // index represents USB keyboard usage value, content is Mac virtual keycode
  344. static UInt8    USBKMAPNumLockUp[256] = {  
  345.     0xFF,     /* 00 no event */        
  346.     0xFF,    /* 01 ErrorRollOver */    
  347.     0xFF,    /* 02 POSTFail */    
  348.     0xFF,    /* 03 ErrorUndefined */    
  349.     0x00,    /* 04 A */
  350.     0x0B,    /* 05 B */
  351.     0x08,    /* 06 C */
  352.     0x02,    /* 07 D */
  353.     0x0E,    /* 08 E */
  354.     0x03,    /* 09 F */
  355.     0x05,    /* 0A G */
  356.     0x04,    /* 0B H */
  357.     0x22,    /* 0C I */
  358.     0x26,    /* 0D J */
  359.     0x28,    /* 0E K */
  360.     0x25,    /* 0F L */
  361.  
  362.     0x2E,     /* 10 M */        
  363.     0x2D,    /* 11 N */    
  364.     0x1F,    /* 12 O */    
  365.     0x23,    /* 13 P */    
  366.     0x0C,    /* 14 Q */
  367.     0x0F,    /* 15 R */
  368.     0x01,    /* 16 S */
  369.     0x11,    /* 17 T */
  370.     0x20,    /* 18 U */
  371.     0x09,    /* 19 V */
  372.     0x0D,    /* 1A W */
  373.     0x07,    /* 1B X */
  374.     0x10,    /* 1C Y */
  375.     0x06,    /* 1D Z */
  376.     0x12,    /* 1E 1/! */
  377.     0x13,    /* 1F 2/@ */
  378.  
  379.     0x14,     /* 20 3 # */        
  380.     0x15,    /* 21 4 $ */    
  381.     0x17,    /* 22 5 % */    
  382.     0x16,    /* 23 6 ^ */    
  383.     0x1A,    /* 24 7 & */
  384.     0x1C,    /* 25 8 * */
  385.     0x19,    /* 26 9 ( */
  386.     0x1D,    /* 27 0 ) */
  387.     0x24,    /* 28 Return (Enter) */
  388.     0x35,    /* 29 ESC */
  389.     0x33,    /* 2A Delete (Backspace) */
  390.     0x30,    /* 2B Tab */
  391.     0x31,    /* 2C Spacebar */
  392.     0x1B,    /* 2D - _ */
  393.     0x18,    /* 2E = + */
  394.     0x21,    /* 2F [ { */
  395.  
  396.     0x1E,     /* 30 ] } */        
  397.     0x2A,    /* 31 \ | */    
  398.     0xFF,    /* 32 Non-US # and ~ (what?!!!) */    
  399.     0x29,    /* 33 ; : */    
  400.     0x27,    /* 34 ' " */
  401.     0x32,    /* 35 ` ~ */
  402.     0x2B,    /* 36 , < */
  403.     0x2F,    /* 37 . > */
  404.     0x2C,    /* 38 / ? */
  405.     0x39,    /* 39 Caps Lock */
  406.     0x7A,    /* 3A F1 */
  407.     0x78,    /* 3B F2 */
  408.     0x63,    /* 3C F3 */
  409.     0x76,    /* 3D F4 */
  410.     0x60,    /* 3E F5 */
  411.     0x61,    /* 3F F6 */
  412.  
  413.     0x62,     /* 40 F7 */        
  414.     0x64,    /* 41 F8 */    
  415.     0x65,    /* 42 F9 */    
  416.     0x6D,    /* 43 F10 */    
  417.     0x67,    /* 44 F11 */
  418.     0x6F,    /* 45 F12 */
  419.     0x69,    /* 46 F13/PrintScreen */
  420.     0x6B,    /* 47 F14/ScrollLock */
  421.     0x71,    /* 48 F15/Pause */                
  422.     0x72,    /* 49 Insert */
  423.     0x73,    /* 4A Home */
  424.     0x74,    /* 4B PageUp */
  425.     0x75,    /* 4C Delete Forward */
  426.     0x77,    /* 4D End */
  427.     0x79,    /* 4E PageDown */
  428.     0x7C,    /* 4F RightArrow */
  429.  
  430.     0x7B,     /* 50 LeftArrow */        
  431.     0x7D,    /* 51 DownArrow */    
  432.     0x7E,    /* 52 UpArrow */    
  433.     0x47,    /* 53 NumLock/Clear */    
  434.     0x4B,    /* 54 Keypad / */
  435.     0x43,    /* 55 Keypad * */
  436.     0x4E,    /* 56 Keypad - */
  437.     0x45,    /* 57 Keypad + */
  438.     0x4C,    /* 58 Keypad Enter */
  439.     0x77,    /* 59 Keypad 1 = End on MacAlly keyapd*/
  440.     0x7D,    /* 5A Keypad 2 = Down Arrow on MacAlly keypad*/
  441.     0x79,    /* 5B Keypad 3 = Page Down on MacAlly keypad*/
  442.     0x7B,    /* 5C Keypad 4 = Left Arrow on MacAlly keypad*/
  443.     0x57,    /* 5D Keypad 5 */
  444.     0x7C,    /* 5E Keypad 6 = Right Arrow on MacAlly keypad*/
  445.     0x73,    /* 5F Keypad 7 = HOME on MacAlly keypad*/
  446.  
  447.     0x7E,     /* 60 Keypad 8 = Up Arrow on MacAlly keypad*/        
  448.     0x74,    /* 61 Keypad 9 = Page Up on MacAlly keypad*/    
  449.     0x72,    /* 62 Keypad 0 = Insert on MacAlly keypad*/    
  450.     0x75,    /* 63 Keypad . = Delete (forward) on MacAlly keypad*/    
  451.     0xFF,    /* 64 Non-US \ and  | (what ??!!) */
  452.     0x6E,    /* 65 ApplicationKey (not on a mac!)*/
  453.     0x7F,    /* 66 PowerKey  */
  454.     0x51,    /* 67 Keypad = */
  455.     0x69,    /* 68 F13 */
  456.     0x6B,    /* 69 F14 */
  457.     0x71,    /* 6A F15 */
  458.     0xFF,    /* 6B F16 */
  459.     0xFF,    /* 6C F17 */
  460.     0xFF,    /* 6D F18 */
  461.     0xFF,    /* 6E F19 */
  462.     0xFF,    /* 6F F20 */
  463.  
  464.     0x5B,     /* 70 F21 */        
  465.     0x5C,    /* 71 F22 */    
  466.     0x52,    /* 72 F23 */    
  467.     0x41,    /* 73 F24 */    
  468.     0xFF,    /* 74 Execute */
  469.     0xFF,    /* 75 Help */
  470.     0x7F,    /* 76 Menu */
  471.     0x4C,    /* 77 Select */
  472.     0x69,    /* 78 Stop */
  473.     0x6B,    /* 79 Again */
  474.     0x71,    /* 7A Undo */
  475.     0xFF,    /* 7B Cut */
  476.     0xFF,    /* 7C Copy */
  477.     0xFF,    /* 7D Paste */
  478.     0xFF,    /* 7E Find */
  479.     0xFF,    /* 7F Mute */
  480.     
  481.     0xFF,     /* 80 no event */        
  482.     0xFF,    /* 81 no event */    
  483.     0xFF,    /* 82 no event */    
  484.     0xFF,    /* 83 no event */    
  485.     0xFF,    /* 84 no event */
  486.     0xFF,    /* 85 no event */
  487.     0xFF,    /* 86 no event */
  488.     0xFF,    /* 87 no event */
  489.     0xFF,    /* 88 no event */
  490.     0xFF,    /* 89 no event */
  491.     0xFF,    /* 8A no event */
  492.     0xFF,    /* 8B no event */
  493.     0xFF,    /* 8C no event */
  494.     0xFF,    /* 8D no event */
  495.     0xFF,    /* 8E no event */
  496.     0xFF,    /* 8F no event */
  497.  
  498.     0xFF,     /* 90 no event */        
  499.     0xFF,    /* 91 no event */    
  500.     0xFF,    /* 92 no event */    
  501.     0xFF,    /* 93 no event */    
  502.     0xFF,    /* 94 no event */
  503.     0xFF,    /* 95 no event */
  504.     0xFF,    /* 96 no event */
  505.     0xFF,    /* 97 no event */
  506.     0xFF,    /* 98 no event */
  507.     0xFF,    /* 99 no event */
  508.     0xFF,    /* 9A no event */
  509.     0xFF,    /* 9B no event */
  510.     0xFF,    /* 9C no event */
  511.     0xFF,    /* 9D no event */
  512.     0xFF,    /* 9E no event */
  513.     0xFF,    /* 9F no event */
  514.  
  515.     0xFF,     /* A0 no event */        
  516.     0xFF,    /* A1 no event */    
  517.     0xFF,    /* A2 no event */    
  518.     0xFF,    /* A3 no event */    
  519.     0xFF,    /* A4 no event */
  520.     0xFF,    /* A5 no event */
  521.     0xFF,    /* A6 no event */
  522.     0xFF,    /* A7 no event */
  523.     0xFF,    /* A8 no event */
  524.     0xFF,    /* A9 no event */
  525.     0xFF,    /* AA no event */
  526.     0xFF,    /* AB no event */
  527.     0xFF,    /* AC no event */
  528.     0xFF,    /* AD no event */
  529.     0xFF,    /* AE no event */
  530.     0xFF,    /* AF no event */
  531.  
  532.     0xFF,     /* B0 no event */        
  533.     0xFF,    /* B1 no event */    
  534.     0xFF,    /* B2 no event */    
  535.     0xFF,    /* B3 no event */    
  536.     0xFF,    /* B4 no event */
  537.     0xFF,    /* B5 no event */
  538.     0xFF,    /* B6 no event */
  539.     0xFF,    /* B7 no event */
  540.     0xFF,    /* B8 no event */
  541.     0xFF,    /* B9 no event */
  542.     0xFF,    /* BA no event */
  543.     0xFF,    /* BB no event */
  544.     0xFF,    /* BC no event */
  545.     0xFF,    /* BD no event */
  546.     0xFF,    /* BE no event */
  547.     0xFF,    /* BF no event */
  548.  
  549.     0xFF,     /* C0 no event */        
  550.     0xFF,    /* C1 no event */    
  551.     0xFF,    /* C2 no event */    
  552.     0xFF,    /* C3 no event */    
  553.     0xFF,    /* C4 no event */
  554.     0xFF,    /* C5 no event */
  555.     0xFF,    /* C6 no event */
  556.     0xFF,    /* C7 no event */
  557.     0xFF,    /* C8 no event */
  558.     0xFF,    /* C9 no event */
  559.     0xFF,    /* CA no event */
  560.     0xFF,    /* CB no event */
  561.     0xFF,    /* CC no event */
  562.     0xFF,    /* CD no event */
  563.     0xFF,    /* CE no event */
  564.     0xFF,    /* CF no event */
  565.  
  566.     0xFF,     /* D0 no event */        
  567.     0xFF,    /* D1 no event */    
  568.     0xFF,    /* D2 no event */    
  569.     0xFF,    /* D3 no event */    
  570.     0xFF,    /* D4 no event */
  571.     0xFF,    /* D5 no event */
  572.     0xFF,    /* D6 no event */
  573.     0xFF,    /* D7 no event */
  574.     0xFF,    /* D8 no event */
  575.     0xFF,    /* D9 no event */
  576.     0xFF,    /* DA no event */
  577.     0xFF,    /* DB no event */
  578.     0xFF,    /* DC no event */
  579.     0xFF,    /* DD no event */
  580.     0xFF,    /* DE no event */
  581.     0xFF,    /* DF no event */
  582.  
  583.     0x3B,     /* E0 left control key */        
  584.     0x38,    /* E1 left shift key key */    
  585.     0x3A,    /* E2 left alt/option key */    
  586.     0x37,    /* E3 left GUI (windows/cmd) key */    
  587.     
  588.     0x3E,    /* E4 right control key */
  589.     0x3C,    /* E5 right shift key key */
  590.     0x3D,    /* E6 right alt/option key */
  591.     0x37,    /* E7 right GUI (windows/cmd) key */
  592.     0xFF,    /* E8 no event */
  593.     0xFF,    /* E9 no event */
  594.     0xFF,    /* EA no event */
  595.     0xFF,    /* EB no event */
  596.     0xFF,    /* EC no event */
  597.     0xFF,    /* ED no event */
  598.     0xFF,    /* EE no event */
  599.     0xFF,    /* EF no event */
  600.     
  601.     0xFF,     /* F0 no event */        
  602.     0xFF,    /* F1 no event */    
  603.     0xFF,    /* F2 no event */    
  604.     0xFF,    /* F3 no event */    
  605.     0xFF,    /* F4 no event */
  606.     0xFF,    /* F5 no event */
  607.     0xFF,    /* F6 no event */
  608.     0xFF,    /* F7 no event */
  609.     0xFF,    /* F8 no event */
  610.     0xFF,    /* F9 no event */
  611.     0xFF,    /* FA no event */
  612.     0xFF,    /* FB no event */
  613.     0xFF,    /* FC no event */
  614.     0xFF,    /* FD no event */
  615.     0xFF,    /* FE no event */
  616.     0xFF,    /* FF no event */
  617. };
  618.         
  619. void
  620. InitUSBKeyboard()
  621. {
  622.     handleKCHR = GetResource('KCHR',0);    // US keyboard mapping (handled differently by ADB Mgr)
  623.     HLock(handleKCHR);
  624.     KCHRptr = (UInt8 *)*handleKCHR;
  625. }
  626.  
  627.  
  628.  
  629. void PostUSBKeyToMac(UInt16 rawUSBkey)
  630. {
  631. static    UInt8    oldLEDState = 0x00;
  632. static    UInt8    newLEDState = 0x00;
  633.  
  634. static    UInt8    capsLockState = 0x00;
  635. static    UInt8    numLockState = 0x00;
  636. static    UInt8    scrollLockState = 0x00;
  637.  
  638. register UInt8    virtualKeycode, keystate;
  639.  
  640.     
  641.     if (KCHRptr == 0)
  642.     {
  643.         InitUSBKeyboard();
  644.     }
  645.     
  646.     keystate = (rawUSBkey & 0x8000) ? UP : DOWN;
  647.     rawUSBkey &= 0x0FF;
  648.     
  649.     if (keystate == DOWN)
  650.     {
  651.         newLEDState = oldLEDState;
  652.         
  653.         switch (rawUSBkey)
  654.         {
  655. // Note:  This switch statement is being left it to make it easy to add "toggling" keys in the future.
  656. //        it used to support toggled numlock & scroll lock...  It doesn't anymore.
  657.             case kUSBNumLockKey:
  658.                 newLEDState ^= (1 << kNumLockLED);
  659.                 keystate = (newLEDState & (1 << kNumLockLED)) ? DOWN : UP;
  660.                 break;
  661.         }
  662.         
  663.         if (newLEDState != oldLEDState)
  664.         {
  665.             oldLEDState = newLEDState;
  666.             //USBHIDControlDevice(kHIDSetLEDStateByBits, &newLEDState);
  667.         }
  668.     }
  669.     
  670.     if (rawUSBkey == kUSBNumLockKey)
  671.     {
  672.         return;
  673.     }
  674.     
  675.     // look up rawUSBkey in KMAP resource to get virtual keycode
  676.     if (rawUSBkey < sizeof(USBKMAPNumLockDown))
  677.     {
  678.         if (oldLEDState & kNumLockLEDMask)
  679.         {
  680.             virtualKeycode = USBKMAPNumLockDown[rawUSBkey];
  681.         }
  682.         else
  683.         {
  684.             virtualKeycode = USBKMAPNumLockUp[rawUSBkey];
  685.         }
  686.     } 
  687.     else 
  688.     {
  689. //        DebugStr("\pPostUSBKeyToMac: Need bigger KMAP table");
  690.         virtualKeycode = 0xFF;
  691.     }
  692.  
  693.     PostADBKeyToMac(virtualKeycode, keystate);
  694.     if (virtualKeycode == 0x7F)
  695.         PostADBKeyToMac(virtualKeycode, keystate);
  696. }
  697.  
  698. void 
  699. PostADBKeyToMac(UInt16 virtualKeycode, UInt8 state)
  700. {
  701.     UInt32    keyEventMsg;
  702.     
  703.     if (virtualKeycode > 127) return;  // not handled by MacOS!
  704.     
  705.     // stop repeating
  706.     LMSetKeyLast(0);
  707.     LMSetKbdVars(0);
  708.     
  709.     // update our keymap
  710.     SetBit((UInt8 *)myKeyMAP, virtualKeycode, state == DOWN ? 1 : 0);
  711.     LMSetKeyMap(&myKeyMAP);
  712.  
  713.     // set this keyboard as the last keyboard
  714.     LMSetKbdLast(FakeADBAddr);
  715.     LMSetKbdType(FakeKBDType);
  716.     
  717.     // call KeyTrans to get character code
  718.     virtualKeycode |= ((myKeyMAP[1]<<9) & 0x00FE00) | ((myKeyMAP[1]>>7) & 0x0100) | ((state==UP) ? 0x080 : 0);
  719.     keyEventMsg = KeyTranslate(KCHRptr, virtualKeycode , &keyTransState);
  720.     virtualKeycode &= 0x7F;
  721.     
  722.     if (keyEventMsg & 0xFFFF0000) {
  723.         // post event
  724.         UInt32 event =(keyEventMsg & 0xFF000000) | ((FakeADBAddr << 16) & 0x0FF0000) | ((virtualKeycode << 8) & 0x0FF00)  | ((keyEventMsg>>16) & 0x0FF);
  725.         if (state == DOWN){
  726.             UInt32 ticks = TickCount();
  727.             LMSetKeyTime(ticks);
  728.             LMSetKeyRepTime(ticks);
  729.             LMSetKeyLast(event & 0x0FFFF);
  730.             LMSetKbdVars((event>>16) & 0x0FFFF);
  731.         }        
  732.         PostEvent((state == DOWN ? keyDown : keyUp), event);
  733.     }
  734.     if (keyEventMsg & 0x0000FFFF) {
  735.         // post event
  736.         UInt32 event =((keyEventMsg<<16) & 0xFF000000) | ((FakeADBAddr << 16) & 0x0FF0000) | ((virtualKeycode << 8) & 0x0FF00)  | (keyEventMsg & 0x0FF);
  737.         if (state == DOWN){
  738.             UInt32 ticks = TickCount();
  739.             LMSetKeyTime(ticks);
  740.             LMSetKeyRepTime(ticks);
  741.             LMSetKeyLast(event & 0x0FFFF);
  742.             LMSetKbdVars((event>>16) & 0x0FFFF);
  743.         }        
  744.         PostEvent((state == DOWN ? keyDown : keyUp), event);
  745.     }
  746. }
  747.  
  748. // Sets the bitmapArray[index] to value
  749. // returns old value;
  750. Boolean
  751. SetBit(UInt8 *bitmapArray, UInt16 index, Boolean value)
  752. {    
  753.     UInt32    mask = 0x1 << (index % 8 );
  754.     Boolean    oldVal;
  755.     
  756.     oldVal = (bitmapArray[index/8] & mask) ? TRUE : FALSE;
  757.     
  758.     if (value){
  759.         bitmapArray[index/8] |= mask;
  760.     }else{
  761.         bitmapArray[index/8] &= ~mask;
  762.     }
  763.     
  764.     return (oldVal);
  765. }
  766.  
  767. void USBDemoKeyIn(UInt32 refcon, void * theData)
  768. {
  769. #pragma unused (refcon)
  770.  
  771. USBHIDDataPtr    pTheKeyboardData;
  772. register        UInt8    i;
  773.  
  774.     pTheKeyboardData = (USBHIDDataPtr)theData;
  775.     for (i=0; i<pTheKeyboardData->kbd.keycount; i++)
  776.     {
  777.         // no shim installed, let's just post some Macintosh keyevents
  778.         PostUSBKeyToMac(pTheKeyboardData->kbd.usbkeycode[i]);
  779.     }
  780.  
  781. }
  782.  
  783. OSStatus USBHIDControlDevice(UInt32 theControlSelector, void * theControlData)
  784. {
  785.     switch (theControlSelector)
  786.     {
  787.         case kHIDSetLEDStateByBits:
  788.             shimParamBlock.hidReport[0] = *(UInt8*)theControlData;
  789.             shimParamBlock.hidReport[1] = *(UInt8*)theControlData;
  790.  
  791.             shimParamBlock.retryCount = kKeyboardRetryCount;
  792.             shimParamBlock.delayLevel = 0;                            
  793.             shimParamBlock.transDepth = 0;    
  794.             
  795.             shimParamBlock.pb.usbRefcon = kSetKeyboardLEDs;                    /* Start with setting the interface protocol */
  796.             InitiateTransaction(&shimParamBlock.pb);
  797.             break;
  798.             
  799.         case kHIDEnableDemoMode:
  800.             USBExpertStatus(myParamBlock.deviceRef, "\pUSBHIDMacAllyTenKey: Demo Mode Enabled", myParamBlock.deviceRef);
  801.             break;
  802.  
  803.         case kHIDDisableDemoMode:
  804.             USBExpertStatus(myParamBlock.deviceRef, "\pUSBHIDMacAllyTenKey: Demo Mode Disabled", myParamBlock.deviceRef);
  805.             break;
  806.  
  807.         default:
  808.             return paramErr;
  809.     }
  810.     return 0;
  811. }
  812.  
  813. void ProcessInterruptReport(UInt8 hidReport[])
  814. {
  815.  
  816. UInt8    i, j, newkey, oldkey, deltas;
  817.  
  818. UInt8        changedmodifiers, keycount;
  819. USBHIDData    theKeyboardData;
  820. Boolean        keypressedflag, keyreleasedflag;
  821.     
  822.     deltas = 0;
  823.     
  824.     if (myParamBlock.hidEmulationInit == false)
  825.     {
  826.         myParamBlock.hidEmulationInit = true;
  827.         for (i = 0; i < kKeyboardReportSize; i++)
  828.             myParamBlock.oldHIDReport[i] = 0;
  829.     };
  830.     
  831.     myParamBlock.oldHIDReport[1] = 0x0;
  832.     hidReport[1] = 0x0;
  833.     
  834.     for (i = 0; i < kKeyboardReportSize; i++)
  835.     {
  836.         if (hidReport[i] != myParamBlock.oldHIDReport[i])
  837.         {
  838.             deltas++;
  839.         }
  840.     }
  841.     
  842.     if ((myParamBlock.sendRawReportFlag) && deltas)
  843.     {
  844.         USBDemoKeyIn(0xff, (void *)&hidReport[0]);
  845.     }
  846.     else
  847.     {
  848.         if (deltas)
  849.         {
  850.             keycount = 0;
  851.             changedmodifiers = hidReport[0] ^  myParamBlock.oldHIDReport[0];
  852.             
  853.             if (changedmodifiers)
  854.             {
  855.                 for (i = 0; i < kKeyboardModifierBits; i++)
  856.                 {
  857.                     if (changedmodifiers & (1 << i))
  858.                     {
  859.                         if (hidReport[0] & (1 << i))
  860.                         {
  861.                             theKeyboardData.kbd.usbkeycode[keycount++] = (0xe0 + i);
  862.                         }
  863.                         else
  864.                         {
  865.                             theKeyboardData.kbd.usbkeycode[keycount++] = (0x80e0 + i);
  866.                         }
  867.                     }
  868.                 }
  869.                 myParamBlock.oldHIDReport[0] = hidReport[0];
  870.             }
  871.             
  872.             if ((hidReport[kKeyboardOffsetToKeys] == 0) || (hidReport[kKeyboardOffsetToKeys] > 0x03))
  873.             {
  874.                 // While this double loop may look strange, I refer you to appendix C, of the HID Devices specification.
  875.                 // pp. 73 & 74 clearly state that report order is abitrary and does not reflect the order of events.
  876.                 // to quote: "The order of keycodes in array fields has no significance.  Order determination is done
  877.                 // by the host software comparing the contents of the previous report to the current report.  If two or 
  878.                 // more keys are pressed in one report, their order is indeterminate.  Keyboards may buffer events that
  879.                 // would have otherwise resulted in multiple events in a single report".
  880.                 
  881.                 // Because this specification (or lack thereof) states that the order is indeterminant, we have to 
  882.                 // check all the old keys against all the new keys...
  883.                 
  884.                 for (i = kKeyboardOffsetToKeys; i < (kKeyboardOffsetToKeys + kKeyboardReportKeys); i++)
  885.                 {
  886.                     keypressedflag = true;
  887.                     newkey = hidReport[i];
  888.                     
  889.                     keyreleasedflag = true;
  890.                     oldkey = myParamBlock.oldHIDReport[i];                            
  891.                     for (j = kKeyboardOffsetToKeys; j < (kKeyboardOffsetToKeys + kKeyboardReportKeys); j++)                                // then look through all the keys that were previously and are currently reported as pressed
  892.                     {                                    
  893.                         if (newkey == myParamBlock.oldHIDReport[j])        // was this new key already pressed?
  894.                         {                                
  895.                             keypressedflag = false;                        // if it was, then don't report it as being pressed.
  896.                         }
  897.                         
  898.                         if (oldkey == hidReport[j])                        // Is the old key still pressed?
  899.                         {                                
  900.                             keyreleasedflag = false;                    // If yes, then don't report it as being released
  901.                         }
  902.                     }
  903.                     if ((newkey > 0x03) && keypressedflag)
  904.                     {
  905.                         theKeyboardData.kbd.usbkeycode[keycount++] = newkey;
  906.                     }
  907.                     
  908.                     if ((oldkey > 0x03) && keyreleasedflag)
  909.                     {
  910.                         theKeyboardData.kbd.usbkeycode[keycount++] = (oldkey + 0x8000);
  911.                     }
  912.                     
  913.                     if (keycount > 20)                                    // a worse case scenario is:
  914.                     {                                                    // 4 modifier keys up
  915.                         break;                                            // the other 4 modifier keys down
  916.                     }                                                    // 6 previously reported keys up
  917.                 }                                                        // 6 newly reported keys down
  918.                                                                         // 6 + 6 + 4 + 4 = 20
  919.                 for (i = 0; i<kKeyboardReportSize; i++)
  920.                 {
  921.                     myParamBlock.oldHIDReport[i] = hidReport[i];
  922.                 };
  923.             }
  924.             
  925.             if (keycount)
  926.             {
  927.                 theKeyboardData.kbd.keycount = keycount;
  928.                 USBDemoKeyIn(myParamBlock.interruptRefcon, (void *)&theKeyboardData);
  929.             }
  930.         }
  931.     }
  932. }
  933.  
  934.  
  935.